package com.fallsvc.dayproblem.demo202511;

import java.util.*;

/**
 * @auther falls_vc
 * description:
 * @date 2025/11/10  15:22
 */
public class Demo20251110 {
    //    https://leetcode.cn/problems/sort-array-by-parity/description/
//    按奇偶排序数组
    public int[] sortArrayByParity(int[] nums) {
        if (nums == null || nums.length == 0) return null;
        // 双指针
        int l = 0;
        int r = nums.length - 1;
        while (l < r) {
            while (l < r && nums[l] % 2 == 0) l++;
            while (l < r && nums[r] % 2 == 1) r--;

            int tmp = nums[l];
            nums[l] = nums[r];
            nums[r] = tmp;
            l++;
            r--;
        }
        return nums;
    }

/**
 * Sorts an array of integers by placing all even numbers first, followed by all odd numbers.
 * This method creates a new array to store the result, maintaining the relative order of even and odd numbers.
 *
 * @param nums The input array of integers to be sorted by parity
 * @return A new array with even numbers first, followed by odd numbers, preserving their relative order
 */
    public int[] sortArrayByParity2(int[] nums) {
        int n = nums.length, index = 0; // Initialize array length and result array index
        int[] res = new int[n]; // 使用空间
        for (int num : nums) {
            if (num % 2 == 0) {
                res[index++] = num;
            }
        }
        for (int num : nums) {
            if (num % 2 == 1) {
                res[index++] = num;
            }
        }
        return res;
    }

//    https://leetcode.cn/problems/VabMRr/

 //    找到字符串所有字母异位词
 public List<Integer> findAnagrams(String ss, String pp) {
        char[] s = ss.toCharArray();
        char[] p = pp.toCharArray();

        int sLen = s.length;
        int pLen = p.length;
        List<Integer> ret = new ArrayList<>();
        if(sLen < pLen) return ret;

        int[] sCount = new int[26];
        int[] pCount = new int[26];
        for (int i = 0; i < pLen; i++) {
            sCount[s[i] - 'a']++;
            pCount[p[i] - 'a']++;
        }
        if(Arrays.equals(sCount,pCount))
            ret.add(0);
        for (int i = pLen; i < sLen; i++) {
            sCount[s[i] - 'a']++;// 向后移动
            sCount[s[i - pLen] - 'a']--;// 出第一个元素
            if(Arrays.equals(sCount,pCount))
                ret.add(i - pLen + 1);
        }
        return ret;
    }

    public static void main(String[] args) {
// Create a HashMap to store key-value pairs with Integer keys and Integer values
        Map<Integer,Integer> map = new HashMap<>();
// Create a simple entry (key-value pair) using AbstractMap.SimpleEntry
// This entry contains key=1 and value=1
        Map.Entry<Integer,Integer> entry = new AbstractMap.SimpleEntry<>(1,1);
// Retrieve the key from the entry
        entry.getKey();

        // 找到字符串所有字母异位词


    }
}
